'use strict'

import {
  app,
  BrowserWindow,
  // Menu,
  Tray
} from 'electron'
import '../renderer/store'
const path = require('path')
let tray = null
/**
 * Set `__static` path to static files in production
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
 */
if (process.env.NODE_ENV !== 'development') {
  global.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
}

let mainWindow
const electron = require('electron')
const winURL = process.env.NODE_ENV === 'development'
  ? `http://localhost:9080`
  : `file://${__dirname}/index.html`
const Menu = electron.Menu

function createWindow () {
  /**
	 * Initial window options
	 */
  Menu.setApplicationMenu(null)
  mainWindow = new BrowserWindow({
    height: 563,
    useContentSize: true,
    width: 650,
    minWidth: 650,
	minHeight: 563,
    frame: false,
    webPreferences: {
      nodeIntegration: true // 注入node模块
    }
  })

  mainWindow.loadURL(winURL)

  mainWindow.on('closed', () => {
    mainWindow = null
  })

  tray = new Tray(path.join(__static, '/logo.png'))
  const contextMenu = Menu.buildFromTemplate([{
    label: '退出',
    click: function () {
      app.quit()
    }
  }])
  tray.setToolTip('web服务管理器')
  tray.setContextMenu(contextMenu)
  tray.on('click', function () {
		 mainWindow.show()
  })
  tray.on('double-click', function () {
	    mainWindow.show()
  })
}

app.on('ready', createWindow)

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  if (mainWindow === null) {
    createWindow()
  }
})

/**
 * Auto Updater
 *
 * Uncomment the following code below and install `electron-updater` to
 * support auto updating. Code Signing with a valid certificate is required.
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-electron-builder.html#auto-updating
 */

/*
import { autoUpdater } from 'electron-updater'

autoUpdater.on('update-downloaded', () => {
  autoUpdater.quitAndInstall()
})

app.on('ready', () => {
  if (process.env.NODE_ENV === 'production') autoUpdater.checkForUpdates()
})
 */
